<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>BendTech Directory Editor</title>

    <?php include_once 'PP.php'; ?>
    <?php include_once 'PPEntry.php'; ?>
    <?php
    // Get/create the entry object 
    $entry = PPEntry::get_current_entry();
    $entry = $entry ? $entry : new PPEntry(NULL);

    // Authenticate user with entry
    $entry->auth(md5($_POST['password']));
    ?>
  </head>

  <body id="pp_edit">
    <?php include_once('bendtech-header.php') ?>

    <?php if (!$entry->isEditable()) { ?>
      <form action="" method="POST">
      <p>This entry is password protected.</p>
      <p>
      Password <input type="password" name="password" />
      </p>
      <input type="button" value="Back" onclick="history.back()"/>
      <input type="submit" value="Continue"/>
      <p style="margin-top: 20px; font-size:10px">
        Forgot your password? <a href="#" onclick="document.getElementById('reset_form').style.display=''">Click here to reset the password</a>
      </p>
      </form>
      <div>
      <form id="reset_form" style="display:none" action="<?= $entry->getURL('password_reset'); ?>" method="POST">
        <h2>Password Reset Form</h2>
        <p>To reset your password, please confirm the private "contact email" address you provided when creating the entry, then click "Reset Password".  A new password will be emailed to you.</p>
        <label for="address">Contact Email</label>
        <input name="address" type="text" />
        <div class="sep"></div>
        <input type="submit" value="Reset Password" />

      </form>
      </div>
    <?php } else { ?>
      <form id="entry_form" enctype="multipart/form-data" method="POST" action="submit.php">
        <?php if (!$entry->isNew()) { ?>
          <div id="delete_sidebar">
            Don't want to be in the directory anymore?
            <br/>
            <br/>
            <input type="button" value="Delete this entry" onclick="pp.deleteEntry()" />
          </div>
        <?php } ?>
        <input name="id" type="hidden" value="<?= $entry->getHtml('id') ?>" />
        <input name="auth" type="hidden" value="<?= $entry->getHtml('auth') ?>" />
        <input name="doDelete" type="hidden" value="0" />

        <p>Important: By using this service you are agreeing to the <a href="terms.php">BendTech Terms of Service, Content Policy, and Privacy Policy</a>.</p>

        <h2>Entry Info</h2>
        <p>Please enter the information you would like to have shown in our public directory.</p>

        <div class="sep"></div>
        <label for="company">Company</label>
        <input name="company" type="text" value="<?= $entry->getHtml('company') ?>" />
        <span class="hint">E.g. GloSoft Technologies. (Leave blank for an individual/consultant listing)</span>

        <div class="sep"></div>
        <label for="name">Name</label>
        <input name="name" type="text" value="<?= $entry->getHtml('name') ?>" />
        <span class="hint">E.g John Smith. (Leave blank to create a company listing)</span>

        <div class="sep"></div>
        <label for="email">Email</label>
        <input name="email" type="text" value="<?= $entry->getHtml('email') ?>" />
        <span class="hint">E.g. info@glosoft.com</span>

        <div class="sep"></div>
        <label for="url">URL</label>
        <input name="url" type="text" value="<?= $entry->getHtml('url') ?>" />
        <span class="hint">E.g. http://www.glosoft.com</span>

        <div class="sep"></div>
        <label for="phone">Phone</label>
        <input name="phone" type="text" value="<?= $entry->getHtml('phone') ?>" />
        <span class="hint">E.g. 541-555-1212</span>

        <div class="sep"></div>
        <span class="hint" style="float:right; width:150px">Hint: If you'd like this entry to appear on our Google Map, please <a href="javascript:void 0" onclick="verifyAddress()">verify that Google recognizes this location</a> (opens in new window)</span>
        <label for="address">Full Address</label>
        <textarea name="address" rows="4"><?= $entry->getHtml('address') ?></textarea>
        <span class="hint">Include city, state, and zip.  E.g.:
        <br />&nbsp;&nbsp;&nbsp;&nbsp;123 Wall St.
        <br />&nbsp;&nbsp;&nbsp;&nbsp;Bend, OR 97701
        </span>

        <div class="sep"></div>
        <label for="image">Logo/Photo</label>
        <input name="image" type="file" />
        <span class="hint">Use jpg, gif or png image (smaller than 1MB, please)</span>

        <div class="sep"></div>
        <label for="description">Description</label>
        <span class="hint">Additional info, if desired (<span id="description_left">1000</span> characters left)</span>
        <textarea id="description" name="description" rows="16"><?= $entry->getHtml('description') ?></textarea>

        <h2>Private Info</h2>
        <p>This information helps us maintain the quality and security of the directory. It is private and will never be shared with anyone.</p>
        <label for="owner">Contact Email</label>
        <input name="owner" type="text" value="<?= $entry->getHtml('owner') ?>" />
        <span class="hint">Email address to contact if we have any questions or issues about this entry. (This is not published or shared)</span>

        <div class="sep"></div>
        <label for="password">Password</label>
        <input name="password" type="password" />
        <?php if ($entry->isSecure()) { ?>
          (Clear password?
          <input name="clear_password" type="checkbox" onclick="pp.clearPassword(this.checked)">
          )
        <?php } ?>
        <span class="hint">Choose a password to ask for before allowing changes.  This isn't required, but without it this entry can (and probably will) be changed by anyone.</span>

        <div class="sep"></div>

        <div class="form_controls">
        <input type="button" value="Cancel" onclick="history.back()"/>
        <input type="submit" value="<?= $entry->isNew() ? 'Create' : 'Save' ?>" />
        </div>
      </form>
    <?php } ?>

    <?php include_once('bendtech-footer.php') ?>
  </body>
</html>

<script>
function verifyAddress() {
  var v = document.forms.entry_form.address.value;
  //v = v.replace(/\n/g, ', ');
  var url = 'http://maps.google.com/maps?q='
  window.open(url + escape(v), 'gverify', 'resizable,width=700,height=500');
}

var pp = {
  load: function() {
    setInterval(pp.limitDescription, 500);
  },

  getForm: function() {
    return document.forms.entry_form;
  },

  limitDescription: function() {
    var max = 1000;
    var el = document.getElementById('description');
    if (el) {
      var v = el.value;
      if (v.length > max) {
        el.value = v.substr(0,max);
      }
      document.getElementById('description_left').innerHTML = Math.max(0, max - v.length);
    }
  },

  verifyAddress: function() {
    var v = pp.getForm().address.value;
    v = v.replace(/\n/g, ', ');
    var url = 'http://maps.google.com/maps?q='
    window.open(url + escape(v), 'gverify', 'resizable,width=700,height=500');
  },

  confirmReset: function(event) {
    if (!confirm('Are you sure?  This will email a new password to the entry owner.\n\nClick OK to continue, or click Cancel to abort')) {
      if (event.preventDefault()) {
        event.preventDefault();
      }
      return false;
    }
  },

  clearPassword: function(flag) {
    pp.getForm().password.disabled=flag;
  },

  deleteEntry: function() {
    if (confirm('Are you sure? This can not be undone.\n\nClick "Ok" to proceed')) {
      pp.getForm().doDelete.value = '1';
      pp.getForm().submit();
    }
  },
};

pp.load();
</script>
